/**
 * @author Alfort Robert - Köckinger Martin
 * This Class handles de Decode functions
 * @version 2.1
 */

package at.fhj.itm;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/** Call when Activity Decode is active */
public class QRCodeDecode extends Activity {

	public Button decodeCamera; /** Creating a Button for decoding from Camera */
	public Button decodeFile; /** Creating a Button for decoding from File */
	public static TextView txtOutPut; /** Creating a TextView for output */
	public Camera camera; /** Creating a Camera object */

	/**
	 * On Create sets basic Information
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.decode); /** ContentView is set to Main layout */

		Log.i("Startup:", "Starting Decoder Instance");

		/**
		 * Defining Buttons by ID of a special layout
		 */
		decodeCamera = (Button) findViewById(R.id.button_camera);
		decodeFile = (Button) findViewById(R.id.button_file);
		txtOutPut = (TextView) findViewById(R.id.outPutText);
		txtOutPut.setAutoLinkMask(1); /** setAutoLinkMask to true
		/*
		 * Set Linkify on (Makes links, call redirection...)
		 * @see android.text.util.Linkify
		 */
		Linkify.addLinks(txtOutPut, Linkify.ALL | Linkify.EMAIL_ADDRESSES
				| Linkify.PHONE_NUMBERS);

		Log.i("Initial", "Creating Clicklisteners");
		
		/**
		 * Creating deocde from Camera Listener
		 * @see at.fhj.itm.CameraView.class
		 * @see android.app.Activity;
		 * @see android.content.Intent;
		 */
		decodeCamera.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {

				startActivity(new Intent(getApplicationContext(),
						CameraPreview.class));
//				startActivity(new Intent(getApplicationContext(),
//						CameraView.class));
			}
		});

		/**
		 * Creating deocde from File Listener
		 * @see at.fhj.itm.CameraView.class
		 * @see at.fhj.itm.QRCodeDecode.doLaunchImagePicker(View view)
		 */
		decodeFile.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				doLaunchImagePicker(arg0);
			}
		});
	}

	/**
	 * Creating Fileexplorer "App"
	 * @param view
	 */
	public void doLaunchImagePicker(View view) {

		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				1);
	}

	/**
	 * This function gives us the chance to handle some Data
	 * @see at.fhj.itm.QRDecoder.class
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String filePath = cursor.getString(columnIndex);
			cursor.close();

			
			QRDecoder qrd = new QRDecoder();
			qrd.execute(filePath);
			Log.i("FilePath:", filePath);
		}

	}
}
